package com.woniu.user.controller;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.annotation.RequirePermission;
import com.woniu.annotation.RequireRole;
import com.woniu.dto.EmployeeInfoDto;
import com.woniu.dto.EmployeeNameDTO;
import com.woniu.dto.UserInfoDTO;
import com.woniu.dto.UserWorkerDetailDTO;
import com.woniu.exception.user.UserException;
import com.woniu.form.InsertEmployeeForm;
import com.woniu.form.SelectUserInfoForm;
import com.woniu.form.UpdateUserInfoForm;
import com.woniu.param.InsertUserOrStoreParam;
import com.woniu.param.SelectUserInfoParam;
import com.woniu.param.UpdateUserInfoParam;
import com.woniu.user.service.EmployeeInfoService;
import com.woniu.user.service.UserInfoService;
import com.woniu.user.service.UserService;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.parser.Token;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 维修员工表 前端控制器
 * </p>
 *
 * @author ly
 * @since 2024年12月05日
 */
@RestController
@RequestMapping("/employeeInfo")
@Api(tags = "维修员工信息接口")
public class EmployeeInfoController {

    @Resource
    EmployeeInfoService employeeInfoService;

    @Resource
    UserService userService;

    @PostMapping("/updateEmployeeInfo")
    @ApiOperation("根据维修员登录编辑自己专业")
    @ApiImplicitParam(name = "profession",value ="专业" )
    public Result updateEmployeeInfo(String profession, @RequestHeader("token")String Token) throws Exception {
        Claims claims = JwtUtil.parseToken(Token);
        Integer userId = (Integer) claims.get("userId");
        employeeInfoService.updateEmployeeInfo(userId, profession);
        return Result.getResult().setMsg("编辑维修员数据成功").setCode(200);
    }

    @GetMapping("/selectEmployeeInfo")
    @ApiOperation("查询维修员姓名")
    public Result selectEmployeeInfo() throws Exception {
        List<EmployeeNameDTO> employeeName = employeeInfoService.selectEmployeeById();
        return Result.getResult(List.class).setData(employeeName).setMsg("查询成功").setCode(200);
    }
    /**
     * 查询区域工人列表
     * @param form 查询参数
     * @return 结果集
     * @throws Exception 异常
     */
    @PostMapping("/queryWorkerList")
    @ApiOperation("查询区域工人列表")
    public Result queryWorkerList(@RequestBody SelectUserInfoForm form) throws Exception {
        PageInfo<EmployeeInfoDto> pageInfo =  employeeInfoService.queryWorkerList(BeanUtil.toBean(form, SelectUserInfoParam.class));
        return Result.getResult(PageInfo.class).setCode(200).setMsg("查询列表成功").setData(pageInfo);
    }

    /**
     * 查询区域工人列表
     * @return 结果集
     * @throws Exception 异常
     */
    @GetMapping("/queryWorkerList")
    @ApiOperation("查询区域工人列表")
    @ApiImplicitParam(name = "areaCodeId",value ="区域id" )
    public Result queryWorkerListAreaCode(@RequestParam("areaCodeId") String  areaCodeId) throws Exception {
        List<EmployeeInfoDto> pageInfo =  employeeInfoService.queryWorkerListAreaCode(areaCodeId);
        return Result.getResult(List.class).setCode(200).setMsg("查询列表成功").setData(pageInfo);
    }

    /**
     * 修改工人区域信息
     * @param form 修改参数
     * @return 结果
     * @throws Exception 异常
     */

    @PostMapping("/updateWorkerInfo")
    @ApiOperation("修改工人区域信息")
    @RequireRole("管理员")
    @RequirePermission("修改工人区域")
    public Result updateWorkerInfo(@RequestBody List<UpdateUserInfoForm> form) throws Exception {
        Integer integer = employeeInfoService.updateWorkerInfo(BeanUtil.copyToList(form, UpdateUserInfoParam.class));
        return Result.getResult(Integer.class).setCode(200).setMsg("修改成功").setData(integer);
    }
    /**
     * 修改工人区域信息的状态
     *
     * @param userId 工人的用户ID，用于识别需要修改的工人信息
     * @return 返回修改结果，包括HTTP状态码200和成功消息
     * @throws Exception 如果修改过程中发生错误，抛出异常
     */
    @PutMapping("/updateWorkerInfoStatus")
    @ApiOperation("派单修改工人状态接口")
    @ApiImplicitParam(name = "userId",value ="工人id" )
    public Result updateWorkerInfoStatus(@RequestParam("userId") String userId) throws Exception {
        employeeInfoService.updateEmployeeInfoStatus(userId);
        return Result.getResult().setCode(200).setMsg("修改成功");
    }

    /**
     * 禁用工人
     */
    @GetMapping("/disableWorker")
    @ApiOperation("禁用工人接口")
    @RequireRole("管理员")
    @RequirePermission("禁用工人")
    @ApiImplicitParam(name = "userId",value ="工人id" )
    public Result disableWorker(@RequestParam("userId") String userId) throws Exception {
        employeeInfoService.disableWorker(userId);
        return Result.getResult().setCode(200).setMsg("禁用成功");
    }
    /**
     * 启用工人
     */
    @GetMapping("/enableWorker")
    @ApiOperation("启用工人接口")
    @RequireRole("管理员")
    @RequirePermission("启用工人")
    @ApiImplicitParam(name = "userId",value ="工人id" )
    public Result enableWorker(@RequestParam("userId") String userId) throws Exception {
        employeeInfoService.enableWorker(userId);
        return Result.getResult().setCode(200).setMsg("启用成功");
    }
    /**
     * 禁用工人
     */
    @GetMapping("/deleteWorker")
    @ApiOperation("删除工人接口")
    @RequireRole("管理员")
    @RequirePermission("删除工人")
    @ApiImplicitParam(name = "userId",value ="工人id" )
    public Result deleteWorker(@RequestParam("userId") String userId) throws Exception {
        employeeInfoService.deleteWorker(userId);
        return Result.getResult().setCode(200).setMsg("删除成功");
    }

    @PostMapping("/insertEmployee.api")
    @ApiOperation("新增维修员工接口")
    @RequireRole("管理员")
    @RequirePermission("新增维修员工")
    public Result insertEmployee(@Valid @RequestBody InsertEmployeeForm ief, @RequestHeader("token") String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String operatorUserName = null;
        try {
            operatorUserName = claims.get("adminName").toString();
        } catch (Exception e) {
            throw new UserException("非管理员不得新增账号", 40003);
        }
        InsertUserOrStoreParam insertUserOrStoreParam = BeanUtil.toBean(ief, InsertUserOrStoreParam.class);
        insertUserOrStoreParam.setOperatorUserName(operatorUserName);
        insertUserOrStoreParam.setMark(4);
        userService.insertUserOrFixer(insertUserOrStoreParam);
        return Result.getResult().setMsg("新增成功").setCode(200);
    }
}

